package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCommunity;
import com.muyu.danmo.domain.DanmoCommunityFavorite;
import com.muyu.danmo.mapper.DanmoCommunityFavoriteMapper;
import com.muyu.danmo.mapper.DanmoCommunityMapper;
import com.muyu.danmo.service.IDanmoCommunityFavoriteService;
import com.muyu.danmo.service.IDanmoCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 社区收藏Service业务处理层
 *
 * @author : kunzong
 * @date : 2024-11-19
 */
@Service
public class DanmoCommunityFavoriteServiceImpl implements IDanmoCommunityFavoriteService {

    @Autowired
    private DanmoCommunityFavoriteMapper danmoCommunityFavoriteMapper;

    @Autowired
    private DanmoCommunityMapper danmoCommunityMapper;

    @Override
    public List<DanmoCommunityFavorite> selectDanmoCommunityFavoriteList(DanmoCommunityFavorite danmoCommunityFavorite) {
        return danmoCommunityFavoriteMapper.selectDanmoCommunityFavoriteList(danmoCommunityFavorite);
    }

    @Override
    public DanmoCommunityFavorite queryById(String id) {
        return null;
    }

    @Override
    public int insert(DanmoCommunityFavorite danmoCommunityFavorite) {
        int i = danmoCommunityFavoriteMapper.selectDanmoCommunityFavoriteByUserIdAndPostId(danmoCommunityFavorite);
        if (i > 0) {
            return 0;
        }
        danmoCommunityFavorite.setCreatedTime(DateUtils.getNowDate());
        return danmoCommunityFavoriteMapper.insertDanmoCommunityFavorite(danmoCommunityFavorite);
    }


    /**
     * 批量删除社区收藏
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] ids) {
        int i = danmoCommunityFavoriteMapper.selectDanmoCommunityFavoriteByIds(ids);
        if (i > 0) {
            return 0;
        }
        return danmoCommunityFavoriteMapper.deleteDanmoCommunityFavoriteByIds(ids);
    }

    @Override
    @Transactional // 确保方法是事务性的
    public synchronized Result isCollect(Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 检查用户是否已经收藏帖子
        List<DanmoCommunityFavorite> existingFavorite = danmoCommunityFavoriteMapper.selectDanmoCollectMaterialByIdAndUserId(id, loginUser.getUserid());

        if (existingFavorite.size() == 0) {
            // 检查帖子是否存在以及是否为用户自己创建
            DanmoCommunity postFavorite = danmoCommunityMapper.selectDanmoCommunityById(id);
            if (postFavorite == null) {
                return Result.error("帖子不存在");
            }

            if (postFavorite.getCreateBy() != null && postFavorite.getCreateBy().equals(loginUser.getUserid())) {
                return Result.error("不能收藏自己的帖子");
            }

            // 插入新的收藏记录
            insertFavorite(loginUser.getUserid(), id);
            return Result.success(id, "收藏成功");
        } else {
            // 用户已收藏，进行取消收藏
            for (DanmoCommunityFavorite danmoCommunityFavorite : existingFavorite) {
                danmoCommunityFavoriteMapper.deleteByCommunity(danmoCommunityFavorite.getId());
            }
            return Result.success(1, "取消收藏成功");
        }
    }

    private void insertFavorite(Long userId, Long postId) {
        DanmoCommunityFavorite newFavorite = new DanmoCommunityFavorite();
        newFavorite.setUserId(userId);
        newFavorite.setPostId(postId);
        newFavorite.setFavoriteName("true"); // 可以考虑转换为常量或枚举
        danmoCommunityFavoriteMapper.insertDanmoCommunityFavorite(newFavorite);
    }
}